<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2017/9/20
 * Time: 11:21
 */
use yii\helpers\Url;
use yii\widgets\Pjax;
use yii\widgets\ListView;
use yii\data\ActiveDataProvider;

use frontend\models\Cart;

$cart['total_money'] = Cart::find()
    ->select(["SUM(g.price)"])
    ->from('cart as c')
    ->innerJoin('goods as g', 'g.id = c.gid')
    ->where(['g.status' => 1, 'c.uid' => yii::$app->user->getId() | 1])
    ->asArray()
    ->one()['SUM(g.price)'];

$cart['all_num'] = Cart::find()
    ->select(["SUM(c.num)"])
    ->from('cart as c')
    ->innerJoin('goods as g', 'g.id = c.gid')
    ->where(['g.status' => 1, 'c.uid' => yii::$app->user->getId() | 1])
    ->asArray()
    ->one()['SUM(c.num)'];

?>
<div class="homescreenadd"><i class="homescreendw"></i><span id="showareainfo">定位地址信息...</span><i class="fa fa-angle-right"></i></div>
<div class="homscrsear">
    <input style="    background-position: 3% center;" readonly="" type="text" placeholder="请输入店铺名、商品名">
</div>
<div id="wrapper" style="top:73px; bottom: 118px;">
    <div id="showorderdet">
        <div class="showorderList" style="transition-property: transform; transform-origin: 0px 0px 0px; transform: translate(0px, 0px) translateZ(0px);">
            <div style="background-color: #FAFAFA">
                <div style="color: #4a4a4a;font-size: 14px;font-weight: bold;padding:5px 5px 5px 8px;border-top: 1px solid #cccccc; ">
                    <span id="closecart">　　</span>
                    <span id="emptycart" data-href="<?=Url::toRoute(['cart/empty'])?>" style="float:right">清空购物车</span>
                </div>
            </div>
            <?php
            $query = Cart::find()->where(['uid' => yii::$app->user->getId() | 1]);
            $dataProvider = new ActiveDataProvider([
                'query' => $query,
                'sort' => [
                    'attributes' => ['gid', 'create_time'],
                ],
            ]);?>
            <?php Pjax::begin();?>
            <?=ListView::widget([
                'dataProvider' => $dataProvider,
                'itemOptions' => ['class' => 'item'],
                'itemView' => '_item_view',
                'summary' => false,
            ]);?>
            <?php Pjax::end();?>
        </div>
    </div>
</div>

<div class="ordeilpaymenCon" style="position:absolute; bottom:50px; z-index:99999999999;">

    <div class="ordeilpaymenshop" style="display: none;"><i id="total_count">0</i></div>


    <div data-toggle="modal" data-target="#myModal" class="emptycartBg"></div>


    <div class="ordeilpaymenBox">



        <div class="ordeilpaymen" style="display: none;">
            <ul>
                <li><b>合计：￥<span id="total_money" style="color:#ff6e6e;">0.00</span></b></li>
<!--                <li><span>另需配送费1元</span></li>-->
            </ul>
        </div>




        <div class="emptyordeilpaymen">
            <ul>
                <li><b>购物车是空的</b></li>
            </ul>
        </div>

        <div id="addShopping" class="ordeilpaybut" style="display: none;"><input type="button" value="去支付"></div>
        <div id="showlimit" class="limitordeilpaybut"><input type="button" value="还差5.00元"></div>
    </div>
</div>

<script>
    var total_money = <?= $cart['total_money']; ?>;
    var all_num = <?= $cart['all_num']; ?>;
    function sum(total_money, all_num) {
        if(total_money > 0){
            $('.emptyordeilpaymen, .emptycartBg, .limitordeilpaybut').css('display', 'none');
            $('.ordeilpaymen,.ordeilpaymenshop, .addShopping').css('display', 'block');
            $("#total_money").html(total_money / 100);
            $("#total_count").html(all_num);
        }

        if(total_money > 2000){
            $('#showlimit').css('display', 'none');
            $('#addShopping').css('display', 'block');
        }
    }

    $(function () {
        sum(total_money, all_num);
    });

    /*购物车*/
    $(function () {
        $('#showorderdet').on('click', '.addbtn', function () {
            var timestamp = Date.parse(new Date());
            $.getScript($(this).attr('data-href') + '&timestamp=' + timestamp, function(){
                window.location.reload()
            });
        });

        $('#showorderdet').on('click', '.downdbtn', function () {
            var timestamp = Date.parse(new Date());
            $.getScript($(this).attr('data-href') + '&timestamp=' + timestamp, function(){
                window.location.reload()
            });
        });

        $('#showorderdet').on('click', '.delbtn', function () {
            var timestamp = Date.parse(new Date());
            $.getScript($(this).attr('data-href') + '&timestamp=' +timestamp, function(){
                window.location.reload()
            });
        });

        $("#emptycart").click(function () {
            var timestamp = Date.parse(new Date());
            $.getScript($(this).attr('data-href') + '&timestamp=' +timestamp, function(){
                window.location.reload();
            });

        });
    });
</script>
